package com.zzw.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.reggie.common.R;
import com.zzw.reggie.entity.Employee;
import com.zzw.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * 员工管理
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1.将页面提交过来的代码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提交的用户名查询数据库
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //3.如果没有查询结果则登录失败
        if (emp == null){
            return R.error("登录失败");
        }

        //4.密码比对 如果密码不一致则登录失败
        if (!password.equals(emp.getPassword())){
            return R.error("登录失败");
        }

        //5.查看员工是否禁用 0表示禁用 如果禁用则返回该用户已被禁用
        if (emp.getStatus() == 0){
            return R.error("该账号已被禁用");
        }

        //6.登录成功 将员工id存入session中 并返回登录成功的结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工操作 员工为：{}",employee);
        //1.为员工设置初始密码
        //密码通过md5加密

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //2.设置初始化数据

        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //从session中获取创建人
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        //3.将数据保存进数据库
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 条件分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Employee>> page(Integer page,Integer pageSize,String name){
        log.info("page: {},pageSize: {},name: {}",page,pageSize,name);

        //1.分页构造器
        IPage<Employee> pageInfo = new Page<>(page,pageSize);
        //2.条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //添加过滤条件
        lqw.like(Strings.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        //3.分页查询
        employeeService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    /**
     * 员工信息修改
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());

        long id = Thread.currentThread().getId();
        log.info("当前线程id为=======>{}",id);

        //设置修改人
        /*Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/

        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询...");

        Employee employee = employeeService.getById(id);
        if (employee != null){

            return R.success(employee);
        }
        return R.error("没有查询到对应的员工信息");
    }

}
